package com.fzy.core.acegi;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import org.acegisecurity.ConfigAttributeDefinition;
import org.acegisecurity.ConfigAttributeEditor;
import org.acegisecurity.intercept.web.AbstractFilterInvocationDefinitionSource;
import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;
import org.springframework.util.AntPathMatcher;
import org.springframework.util.PathMatcher;

import com.fzy.core.entity.Resource;
import com.fzy.core.spring.M;
import com.fzy.core.utils.CollectionUtils;




public class DBFilterInvocationDefinitionSource extends
		AbstractFilterInvocationDefinitionSource {
	protected final Logger log = Logger.getLogger(getClass());

	private boolean convertUrlToLowercaseBeforeComparison = false;

	private boolean useAntPath = false;
	
	private final PathMatcher pathMatcher = new AntPathMatcher();	
	
	//private AcegiCacheManager acegiCacheManager;


	public ConfigAttributeDefinition lookupAttributes(String url) {		
//		if (isUseAntPath()) {
//			// Strip anything after a question mark symbol, as per SEC-161.
//			int firstQuestionMarkIndex = url.lastIndexOf("?");
//
//			if (firstQuestionMarkIndex != -1) {
//				url = url.substring(0, firstQuestionMarkIndex);
//			}
//		}
		Map r =M.getResource();

		if (convertUrlToLowercaseBeforeComparison) {
			url = url.toLowerCase();
		}
				
		List roleNameList = new ArrayList();
		for (Iterator iterator = r.keySet().iterator(); iterator.hasNext();) {
			String resString =(String) iterator.next();
			if(StringUtils.isBlank(resString)){
				log.error("Empty resource string of resource :" + resString);
				continue;
			}
			boolean matched = false;
			if (isUseAntPath()) {
				matched = pathMatcher.match(resString, url);
			} else {
		/*		Pattern compiledPattern;
				Perl5Compiler compiler = new Perl5Compiler();
				try {
					compiledPattern = compiler.compile(resString, Perl5Compiler.READ_ONLY_MASK);
				} catch (MalformedPatternException mpe) {
					log.error("Wrong resource string of resource id:" + resString, mpe);
					throw new IllegalArgumentException("Malformed regular expression: " + resString);
				}
				matched = matcher.matches(url, compiledPattern);*/
			}
			if (matched) {
			/*	System.out.println("---------------------------------"+resString+"--------------------------------------");
				ResourceCache rc=acegiCacheManager.getResourceCache();
				List list=rc.getCache().getKeys();
				System.out.println("resource cache size "+list.size());
				for(java.util.Iterator it=list.iterator();it.hasNext();){
					String key= (String)it.next();
					System.out.println("resourceUrl:"+key);
					Element element=rc.getCache().get(key);
					Resource rs=(Resource) element.getValue();
					
					List rl=rs.getRoleNameList();
					for(Iterator itor=rl.iterator();itor.hasNext();){
						String rolename=(String)itor.next();
						System.out.println("role:"+rolename);
					}
				}*/
				
				Resource resource=(Resource)r.get(resString);
				if(resource.getRoleNameList() != null && resource.getRoleNameList().size() > 0){
					roleNameList = (List)CollectionUtils.union(roleNameList, resource.getRoleNameList());
				}				
			}
		}
		if(roleNameList.size() > 0){
			ConfigAttributeEditor configAttrEditor = new ConfigAttributeEditor();
			configAttrEditor.setAsText(CollectionUtils.join(roleNameList));
			return (ConfigAttributeDefinition) configAttrEditor.getValue();
		}
		return null;
	}
	public Iterator getConfigAttributeDefinitions() {
		return null;
	}

	public void setConvertUrlToLowercaseBeforeComparison(
			boolean convertUrlToLowercaseBeforeComparison) {
		this.convertUrlToLowercaseBeforeComparison = convertUrlToLowercaseBeforeComparison;
	}

	public boolean isConvertUrlToLowercaseBeforeComparison() {
		return convertUrlToLowercaseBeforeComparison;
	}

	public boolean isUseAntPath() {
		return useAntPath;
	}

	public void setUseAntPath(boolean useAntPath) {
		this.useAntPath = useAntPath;
	}
/*
	public void setAcegiCacheManager(AcegiCacheManager acegiCacheManager) {
		this.acegiCacheManager = acegiCacheManager;
	}
	*/
}
